package cn.globalph.housekeeper.ui.login.code

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.viewModelScope
import cn.globalph.housekeeper.BuildConfig
import cn.globalph.housekeeper.Event
import cn.globalph.housekeeper.HouseKeeperApplication
import cn.globalph.housekeeper.data.TokenStorage
import cn.globalph.housekeeper.ui.BaseViewModel
import kotlinx.coroutines.*
import java.lang.Exception

const val WAITING_TIME = 120
class LoginWithCodeViewModel (
    private val repository: LoginWithCodeRepository
):BaseViewModel(){
    var userName = MutableLiveData<String>()
    var code = MutableLiveData<String>()

    private var _loginSuccess = MutableLiveData<Event<Boolean>>()
    val loginSuccess:LiveData<Event<Boolean>> = _loginSuccess

    var waiting = MutableLiveData<Int>().apply { value = 0 }

    var btnMsg:LiveData<String> = Transformations.map(waiting){if(it>0) it.toString()+"S" else "获取验证码"}

    var btnEnable = Transformations.map(waiting){it == 0}


    fun getLoginCode(){
        if(userName.value.isNullOrEmpty()){
            _toastEvent.value = Event("请输入手机号")
            return
        }
        viewModelScope.launch {
            try{

                val model = repository.getCode(userName.value!!)
                if(model.isSuccessful()){
                    waiting.value = WAITING_TIME
                    async{
                        while (waiting.value!!>0){
                            delay(1000)
                            waiting.value = waiting.value!!-1
                        }
                    }
                    _toastEvent.value = Event("验证码已发送")
                }else{
                    _toastEvent.value = Event(model.message)
                }
            }catch (e:Exception){
                if(BuildConfig.DEBUG) e.printStackTrace()
                _toastEvent.value = Event("网络异常，请重试")
            }
        }

    }

    fun doLogin(){
        if(userName.value.isNullOrEmpty()){
            _toastEvent.value = Event("请输入用户名")
            return
        }
        if(code.value.isNullOrEmpty()){
            _toastEvent.value = Event("请输入验证码")
            return
        }

        viewModelScope.launch {
            try {
                setLoading()
                val model = repository.loginByCode(userName.value!!,code.value!!)
                setUnLoading()
                if(model.isSuccessful()){
                    TokenStorage.token = model.data!!.token
                    TokenStorage.setUserData(HouseKeeperApplication.instance.applicationContext,model.data!!)
                    _loginSuccess.value = Event(true)
                }else{
                    _toastEvent.value = Event(model.message)
                }

            }catch (e:Exception){
                setUnLoading()
                if(BuildConfig.DEBUG) e.printStackTrace()
                _toastEvent.value = Event("网络异常，请重试")
            }

        }
    }


}